<template>
  <div class="fox">
    <div class="top">
      <div class="top-left">
        1024
      </div>
      <div class="top-morden">
        <div class="morden-left">
          <span class="iconfont icon-icon_bofang"></span>
          <div>正在播放 <span>{{music_play}}</span></div>
          </div>
          <div class="morden-right">
            <span class="iconfont icon-icon-"></span>
            <div>下一首 <span>{{music_next}}</span></div>
        </div>
      </div>
      <div class="right">
        <router-link to="/login">
        退出登录
        </router-link>

      </div>
    </div>
    <div>
      <div>
        <el-row style="position: unset;height: 450px;">
          <el-col :span="10" style="margin-left: 80px;">
            <el-carousel indicator-position="outside">
              <el-carousel-item v-for="item in img" :key="item.id">

               <el-button @click="onForm($event)" >
                 <img :src="item.url" class="img" :title="item.music_type"></el-button>
              </el-carousel-item>
            </el-carousel>
          </el-col>
          <el-col :span="10" style="margin-left: 80px;">
            <el-table :data="tableData" border style="width: 100%" max-height="300px" :row-class-name="tableRowClassName">
              <el-table-column prop="music_name" label="歌曲名" width="120">
              </el-table-column>
              <el-table-column prop="music_singer_name" label="歌手" width="120">
              </el-table-column>
			  <el-table-column prop="music_duration" label="时长" width="120">
			  </el-table-column>
              <el-table-column fixed="right" label="操作" width="100">
                <template slot-scope="scope">
                  <el-button @click="addToPlayList(scope.row)" type="primary" size="small">添加至列表</el-button>

                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
      </div>
    </div>
    <ul class="foot" style="margin: 7.5px; padding: 7.5px;">
      <li class="nav-item">
        <span class="iconfont icon-shouye"></span>
        <div class="navItemTitle">
          <router-link to='main'>
            <div id="button1">首页</div>
          </router-link>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-fuwu"></span>
        <div class="navItemTitle">
          <div id="button2">服务</div>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-yuanchangbanchang"></span>
        <div class="navItemTitle">
          <div id="button3">伴唱</div>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-luyinshuohuashengyin"></span>
        <div class="navItemTitle">
          <div id="button4">录音</div>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-tiaoyin"></span>
        <div class="navItemTitle">
          <div id="button5">调音</div>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-zhongchang"></span>
        <div class="navItemTitle">
          <div id="button6">重唱</div>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-jia"></span>
        <div class="navItemTitle">
          <div id="button7"></div>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-redian"></span>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-jian"></span>
        <div class="navItemTitle">
          <div id="button8"></div>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-sousuo"></span>
        <div class="navItemTitle">
          <router-link to='/search'>
            <div id="button9">搜索</div>

          </router-link>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-bofangheqiege" @click="nextMusic"></span>
        <div class="navItemTitle" @click="nextMusic">
          <div id="button10" @click="nextMusic">切歌</div>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-fenlei"></span>
        <div class="navItemTitle">
          <router-link to='/allsinger'>
            <div id="button11">歌手</div>
          </router-link>
        </div>
      </li>
      <li class="nav-item">
        <span class="iconfont icon-yidian"></span>
        <div class="navItemTitle">
          <el-popover placement="top-end" width="400" trigger="click">
            <el-table :data="Playlist" style="width: 100%" max-height="250" :row-class-name="tableRowClassName">
              <el-table-column prop="music_name" label="歌曲名" width="120">
              </el-table-column>
              <el-table-column prop="music_singer_name" label="歌手" width="120">
              </el-table-column>
              <el-table-column prop="music_duration" label="时长" width="120">
              </el-table-column>
              <el-table-column fixed="right" label="操作" width="130">
                <template slot-scope="scope">
                  <el-button @click.native.prevent="deleteMusicInPlayList(scope.row)" type="text" size="small">
                    移除
                  </el-button>
                  <el-button @click.native.prevent="upRow(scope.row)" type="text" size="small">
                    下一首播放
                  </el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-button slot="reference" @click="showPalyList">列表</el-button>
          </el-popover>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
  import axios from 'axios'
  export default {
    data() {
      return {
		music_file_add: this.GLOBAL.music_file_add, // 当前播放歌曲的文件路径
		isPlay: this.GLOBAL.isPlay, // 当前是否有歌曲在播放
		tableData: [], // 歌曲
        count: 0,
        music_play: this.GLOBAL.music_play,// 当前播放的歌名
        music_next: this.GLOBAL.music_next,// 下一首要播放的歌名
        Playlist:this.GLOBAL.playList,  // 播放列表
        img:[
          {id:0,url: require("../assets/img/img1.png"), music_type: "伤感"},
          {id:1,url:require("../assets/img/img5.png"), music_type: "流行"},
          {id:2,url:require("../assets/img/img3.png"), music_type: "爱情"},
          {id:3,url:require("../assets/img/img4.jpg"), music_type: "古风"},
        ],

      }

    },
    methods: {
	  logOut(){
		console.log('logOut')
		axios.delete(`/api/music/logout/`)
		  .then((resultData) =>{
			  var status = resultData.data.status
			  if(status===204){

				this.$message({
	  						    message: resultData.data.message,
	  						    type: 'success'
	  						    })
				this.$router.push({name:"Login"})
				} else {
					this.$message({
								    message: resultData.data.message,
								    type: 'error'
								    })
				}
		    })
		  .catch()
	  },
	  nextMusic(){
	  		// console.log(next_music)
	  		// 切歌
	  		var music_name = this.Playlist[0].music_name
	  		var music_singer_name = this.Playlist[0].music_singer_name
	  		var music_duration = this.Playlist[0].music_duration
	  		var row = {
	  			"music_name":music_name,
	  			"music_singer_name":music_singer_name,
	  			"music_duration":music_duration
	  		}
			this.isPlay = false
	  		this.deleteMusicInPlayList(row)

	  },
      onForm(event){
		  console.log("onForm")
		  //获取点击对象
		var el = event.currentTarget.firstElementChild
		var music_type = el.innerHTML.split(" ")[2].split("=")[1].split('\"')[1]
		 // alert("当前对象的内容："+el.innerHTML.split(" ")[2].split("=")[1].split('\"')[1]);
		  // 根据用户点击的图片，获取到对应的歌曲类型，同时右边的列表数据修改
		//1.获取用户点击的图片类型
		// console.log(music_type)
		// console.log(`/api/music/type/${music_type}/`)
        axios.get(`/api/music/type/${music_type}/`)
          .then((resultData) =>{
        	  var status = resultData.data.status
        	  if(status===200){
				var music_data = resultData.data
        		this.tableData = music_data.data
				console.log(this.tableData)
        	  }
            })
          .catch()
      },

      onList(){
        const  params1={
          name1:this.tableData.name1,
          sang2: this.tableData.sang2,
        }
        axios.post(`/api/music/music/playlist/show/`,params1)
          .then((resultData) =>{
              console.log(resultData.data)
			  var status = resultData.data.status
			  if(status===200){
				  this.$router.push({"name": "Main"})
			  } else {
					this.$message({
								    message: resultData.data.message,
								    type: 'error'
								    })
				}
            })
          .catch()
      },


	  upRow(row) {
	  		  // 将指定歌曲移到列表第二位
	  		console.log(row)
	  		var music_name = row.music_name
	  		var music_singer_name = row.music_singer_name
	  		var music_duration = row.music_duration
	  		axios.post(`/api/music/playlist/upper/${music_name}/${music_singer_name}/${music_duration}/`)
	  		  .then((resultData) =>{
	  		          	  var status = resultData.data.status

	  		          	  if(status===202){
	  						this.showPalyList();
	  						this.$message({
	  						                message: resultData.data.message,
	  						                type: 'success'
	  						              })


	  		          	  } else {
					this.$message({
								    message: resultData.data.message,
								    type: 'error'
								    })
				}
	  		    })
	  		  .catch()

	  },
	  deleteMusicInPlayList(row){
	  		// 将播放列表中的歌曲删除
	  		// console.log(index)
	  		console.log(row)
	  		var music_name = row.music_name
	  		var music_singer_name = row.music_singer_name
	  		var music_duration = row.music_duration
	  		axios.delete(`/api/music/playlist/del/music/${music_name}/${music_singer_name}/${music_duration}/`)
	  		  .then((resultData) =>{
	  		          	  var status = resultData.data.status

	  		          	  if(status===202){
	  						this.showPalyList();
	  						this.$message({
	  						                message: resultData.data.message,
	  						                type: 'success'
	  						              })


	  		          	  } else {
					this.$message({
								    message: resultData.data.message,
								    type: 'error'
								    })
				}
	  		    })
	  		  .catch()
	  },
	  addToPlayList(row) {
	    console.log(row);
	  		// 将歌曲添加到播放列表中
	  		var music_name = row.music_name
	  		var music_singer_name = row.music_singer_name
	  		var music_duration = row.music_duration
	  		// console.log(music_name)
	  		axios.post(`/api/music/playlist/order/${music_name}/${music_singer_name}/${music_duration}/`)
	  		  .then((resultData) =>{
	  		          	  var status = resultData.data.status

	  		          	  if(status===201){
	  		  				var music_data = resultData.data
	  		          		this.Playlist = music_data.data
	  		  				// console.log(this.Playlist)
	  						// console.log("添加方法")
	  						// console.log(resultData)
	  						this.$message({
	  						                message: resultData.data.message,
	  						                type: 'success'
	  						              })

	  		          	  } else {
					this.$message({
								    message: resultData.data.message,
								    type: 'warning'
								    })
				}
	  		    })
	  		  .catch()
	  },
	  play(){
	  		console.log(this.Playlist.length)
	  		if(!this.isPlay){
	  				console.log(this.isPlay)
	  			var music_name = this.Playlist[0].music_name
	  			var music_singer_name = this.Playlist[0].music_singer_name
	  			// var music_duration = this.Playlist[0].music_duration
	  			axios.get(`/api/music/play/${music_name}/${music_singer_name}/`)
	  			  .then((resultData) =>{
	  						  var status = resultData.data.status
	  						  if(status===200){
	  							 this.music_file_add = resultData.data.music_file_add

	  							 // window.location.href = this.music_file_add
	  							 window.open(this.music_file_add)
	  							 this.isPlay = true
	  							// this.Playlist = music_data.data
	  							// console.log(this.Playlist)


	  						  }
	  				})
	  			  .catch()
	  		}
	  },
	  showPalyList(){
	  	// 获取播放列表中的歌曲
	  	axios.get("/api/music/playlist/show/")
	  	  .then((resultData) =>{
	  	          	  var status = resultData.data.status
	  	          	  if(status===200){
	  	  				var music_data = resultData.data
	  	          		this.Playlist = music_data.data
	  	  				// console.log(this.Playlist)
						if(this.Playlist.length>=1){
							this.music_play = this.Playlist[0].music_name
							if (this.Playlist.length >= 2){
								this.music_next = this.Playlist[1].music_name
							} else{
								this.music_next = ''
							}
							this.play()
						}
						console.log(this.Playlist.length)
						console.log(this.music_play)
						console.log(this.music_next)
	  	          	  }
	  	    })
	  	  .catch()
	  },

	  tableRowClassName({row, rowIndex}){
		  if(rowIndex%2 === 1){
			  return 'warning-row'
		  } else if(rowIndex%2 === 0){
			  return 'success-row'
		  }
		  return ''
	  }

    }, // end methods
	created() {
		// 首页默认流行音乐列表
		axios.get("/api/music/type/流行/")
		  .then((resultData) =>{
			  var music_data = resultData.data
			  // console.log(this.tableData)
			  // music_data.forEach(function(music, index) {
					// console.log(music);
			  // });
			  var status = resultData.data.status
			  if(status===200){
				  this.tableData = music_data.data
			  }
		    })
		  .catch()
		this.showPalyList()

	}, // end created
  }
</script>

<style>
  .fox {
    height: 100%;
    width: 100%;

    background-color: #0077AA;
  }

  .top {
    height: 100px;
    float: left;
    display: flex;
  }

  .top-left {
    padding-left: 0;
    width: 200px;
    dispaly: flex;
    font-size: 50px;
    color: white;
    margin-left: 20px;
  }

  .top-morden {
    padding-left: 200px;
    width: 500px;
    display: flex;
    font-size: 10px;
    color: white;
    align-items: center;
    justify-content: space-around;
  }
  .right{
    padding-left: 30px;
    display: flex;
    align-items: center;
  }

  .el-carousel__item h3 {
    color: #475669;
    font-size: 18px;
    opacity: 0.75;
    line-height: 300px;
    margin: 0;
  }

  .el-carousel__item:nth-child(2n) {
    background-color: #0077AA;
  }

  .el-carousel__item:nth-child(2n+1) {
    background-color: #0077AA;
  }




  .foot {
    height: 100px;
  /*  width: 100%; */
    position: relative;
    bottom: 0;
    align-items: baseline;
    display: flex;
    justify-content: space-between;

    color: white;
    margin-left: 10px;
  }

  .navItemTitle {
    font-size: 10px;
    color: white;
    /*margin-top: 16px;*/

  }
 .a {
    text-decoration: none;
  }

  .router-link-active {
    text-decoration: none;
  }

  .nav-item {
    height: 100px;
    width: 90px;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .nav-item:hover {
    color: #990055;
  }

  .foot .nav-item .iconfont {
    font-size: 20px;
    color: white;
  }

  .el-button {
    background-color: #0077AA;
    border: 0;
    padding: 0;
    color: white;
    font-size: 10px;

  }

  .el-table .warning-row{
	  background: oldlace;
  }

  .el-table .success-row{
  	  background: #f0f9eb;
  }

</style>
